package com.canbot.u05.activity.presidemode.newcode;

import android.app.Dialog;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SimpleItemAnimator;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.canbot.u05.R;
import com.canbot.u05.activity.customvoice.tool.GridSpacingItemDecoration;
import com.canbot.u05.activity.presidemode.newcode.adapter.NChoicePictureEmotionAdapter;
import com.canbot.u05.activity.presidemode.newcode.bean.NModuleType;
import com.canbot.u05.activity.presidemode.newcode.bean.NetLoadUtil;
import com.canbot.u05.activity.presidemode.newcode.bean.PicturnEmotionBean;
import com.canbot.u05.activity.presidemode.newcode.interfaces.IHeadCommonClick;
import com.canbot.u05.activity.presidemode.newcode.interfaces.IItemClick;
import com.canbot.u05.activity.presidemode.newcode.interfaces.IResponse;
import com.canbot.u05.activity.presidemode.newcode.view.HeadCommonView;
import com.canbot.u05.utils.ToastTools;
import com.canbot.u05.widget.LoadingDialogUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class NChoicePictureOrEmotionActivity extends BaseHostActivity {
    public static final String ROUTE = "route";
    @BindView(R.id.headCommonView)
    HeadCommonView headCommonView;
    @BindView(R.id.recycleView)
    RecyclerView recycleView;
    private PicturnEmotionBean picturnEmotionBean;
    private int prevPos;
    private NChoicePictureEmotionAdapter adapter;
    private int intentIntExtra;
    private List<PicturnEmotionBean> beanList;

    @Override
    protected void initEventData() {
        initTitle();
        initCommonHead();
        recycleView.setLayoutManager(new GridLayoutManager(this, 3));
        beanList = new ArrayList<>();
        ((SimpleItemAnimator) recycleView.getItemAnimator()).setSupportsChangeAnimations(false);
        recycleView.addItemDecoration(new GridSpacingItemDecoration(3, (int) getResources().getDimension(R.dimen.x6), false));
        adapter = new NChoicePictureEmotionAdapter(intentIntExtra, beanList, this, new IItemClick() {
            @Override
            public void onClick(int position, View v) {
                if (position != prevPos) {
                    PicturnEmotionBean danceBean = beanList.get(prevPos);
                    danceBean.setSelect(false);
                }
                picturnEmotionBean = beanList.get(position);
                picturnEmotionBean.setSelect(!picturnEmotionBean.isSelect());
                adapter.notifyItemChanged(prevPos);
                adapter.notifyItemChanged(position);
                prevPos = position;
            }
        });
        recycleView.setAdapter(adapter);
        if (intentIntExtra == NModuleType.PICTURE) {
            getDataFromNet();
        } else {
            initData();
        }
    }

    private void initData() {
        beanList.add(new PicturnEmotionBean(NModuleType.EMOTION_XI));
        beanList.add(new PicturnEmotionBean(NModuleType.EMOTION_NU));
        beanList.add(new PicturnEmotionBean(NModuleType.EMOTION_AI));
        beanList.add(new PicturnEmotionBean(NModuleType.EMOTION_SI));
        beanList.add(new PicturnEmotionBean(NModuleType.EMOTION_DAIJI));
        beanList.add(new PicturnEmotionBean(NModuleType.EMOTION_JING));
        beanList.add(new PicturnEmotionBean(NModuleType.EMOTION_KONG));
        adapter.notifyDataSetChanged();
    }

    private void initCommonHead() {
        headCommonView.setiHeadCommonClick(pos -> {
            switch (pos) {
                case IHeadCommonClick.LEFT:
                    finish();
                    break;
                case IHeadCommonClick.RIGHT:
                    if (picturnEmotionBean != null && picturnEmotionBean.isSelect()) {
                        Intent intent = new Intent();
                        intent.putExtra(DATA, picturnEmotionBean);
                        setResult(intentIntExtra, intent);
                        finish();
                    } else {
                        Toast.makeText(this, getString(R.string.picture_null_tip), Toast.LENGTH_SHORT).show();
                    }
                    break;
            }
        });
    }

    private void initTitle() {
        Intent intent = getIntent();
        if (intent != null) {
            intentIntExtra = intent.getIntExtra(ROUTE, NModuleType.EMOTION);
            if (intentIntExtra == NModuleType.PICTURE) {
                headCommonView.setCenterTitle(getString(R.string.choice_picture));
            } else {
                headCommonView.setCenterTitle(getString(R.string.choice_emotion));
            }
        }
    }

    private static final String TAG = "NChoicePictureOrEmotion";
    private Dialog dialogLoading;

    private void getDataFromNet() {
        dialogLoading = LoadingDialogUtils.createLoadingDialog(this, getString(R.string.loading));
        NetLoadUtil.loadPicture(this, new IResponse<String[]>() {
            @Override
            public void onSucusse(String[] result) {
                LoadingDialogUtils.closeDialog(dialogLoading);
                if (result != null) {
                    for (String s : result) { //uupic/9.jpg
                        Log.e(TAG, "onSucusse: " + s);
                        PicturnEmotionBean emotionBean = new PicturnEmotionBean();
                        emotionBean.setType(NModuleType.PICTURE);
                        emotionBean.setFileName(s);
                        beanList.add(emotionBean);
                    }
                    runOnUiThread(() -> adapter.notifyDataSetChanged());
                }
            }

            @Override
            public void onFailuer(String result) {
                LoadingDialogUtils.closeDialog(dialogLoading);
                ToastTools.newInstance(getApplicationContext()).showText(result);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LoadingDialogUtils.closeDialog(dialogLoading);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_nchoice_picture_emotion;
    }


}
